package cn.edu.pku.ss.music.activities;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import cn.edu.pku.ss.music.R;
import cn.edu.pku.ss.music.entities.Music;
import cn.edu.pku.ss.music.util.GlobalResource;
import cn.edu.pku.ss.music.util.ImageUtil;

public class ShareActivity extends Activity {
	private ListView music_listview;

	private TextView title_textview;
	private ImageView share_image;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.share_music_layout);

		music_listview = (ListView) findViewById(R.id.music_listview);
		music_listview.setAdapter(new MusicAdapter(this));

		View layout = findViewById(R.id.share_header);
		title_textview = (TextView) layout.findViewById(R.id.header_title);
		title_textview.setText(R.string.share_title);

		share_image = (ImageView) layout
				.findViewById(R.id.header_right_button_img);
		share_image.setImageResource(R.drawable.bar_share_music);

	}

	class MusicAdapter extends BaseAdapter {
		private LayoutInflater inflater;
		private List<Music> musics;

		public MusicAdapter(Context context) {
			inflater = LayoutInflater.from(context);
			musics = GlobalResource.getInstance().getMusics();
		}

		@Override
		public int getCount() {
			return musics.size();
		}

		@Override
		public Object getItem(int arg0) {
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int location, View arg1, ViewGroup parent) {
			View view = inflater.inflate(R.layout.share_music_list_item_layout,
					null);

			ImageView image = (ImageView) view.findViewById(R.id.music_image);
			Bitmap album_art = ImageUtil.getRoundedCornerBitmap(
					musics.get(location).getAlbum_art(), 15);
			image.setImageBitmap(album_art);

			TextView title = (TextView) view.findViewById(R.id.music_title);
			title.setTextColor(Color.BLACK);
			title.setText(musics.get(location).getTitle());

			TextView singer = (TextView) view.findViewById(R.id.music_singer);
			singer.setText(musics.get(location).getSinger());

			ImageView select_or_not = (ImageView) view
					.findViewById(R.id.select_or_not);
			select_or_not.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					ImageView imageView = (ImageView) view;
					Integer integer = (Integer) imageView.getTag();
					integer = integer == null ? 0 : integer;
					switch (integer) {
					case R.drawable.fav_star:
						imageView.setImageResource(R.drawable.fav_unstar);
						imageView.setTag(R.drawable.fav_unstar);
						break;
					case R.drawable.fav_unstar:
						imageView.setImageResource(R.drawable.fav_star);
						imageView.setTag(R.drawable.fav_star);
						break;
					default:
						imageView.setImageResource(R.drawable.fav_star);
						imageView.setTag(R.drawable.fav_star);
						break;
					}
				}
			});

			return view;
		}
	}
}
